/**
 * 首页
 */
$(function () {
    /**
     * 查询的值
     * @type {string}
     */
    var searchText = "1";
    var initCurrPage = 1;
    var initPageSize = 10;
    var articleUrl = "info.html?id=";
    var activityUrl = "info.html?type=TZGGHDTZ&id=";
    var xqUrl = "/index/lib/Requirements";
    var zjUrl = "/index/lib/experts";
    var qyUrl = "/index/lib/Enterprises";
    var cgUrl = "/index/lib/Harvests";
    var ysUrl = "/index/lib/Colleges";
    var fwUrl = "/index/lib/Services";
    var pondUrl = null;
    var pondDetailUrl = null;
    var type = null;
    var articlePaginationFlag = false;
    var activityPaginationFlag = false;
    var pondPaginationFlag = false;
    // 获取url参数
    var urlParams = getQueryVariable();
    getSearchText();
    initSearch();

    // 获取查询的值
    function getSearchText(){
        searchText = urlParams["text"];
    }
    // 获取参数
    function getQueryVariable() {
        var url = window.location.search; //获取url中"?"符后的字串
        url = decodeURI(url);
        var paramsJson = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                paramsJson[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return paramsJson;
    }

    $('a[data-toggle="tab"]').on('click', function (e) {
        var id = $(e.currentTarget).attr("id");
        if(id == "article"){
            articlePaginationFlag = false;
            searchArticle();
        } else if(id == "hd"){
            activityPaginationFlag = false;
            searchActivity();
        } else {
            pondPaginationFlag = false;
            searchPondByType(id);
        }
    });

    function initSearch(){
        searchArticle();
        searchActivityCount();
        searchPondCount("xq",xqUrl,"xq_count");
        searchPondCount("zj",zjUrl,"zj_count");
        searchPondCount("qy",qyUrl,"qy_count");
        searchPondCount("cg",cgUrl,"cg_count");
        searchPondCount("ys",ysUrl,"ys_count");
        searchPondCount("fw",fwUrl,"fw_count");
    }

    function searchPondByType(id){
        if (id == "xq"){
            pondUrl = "/index/lib/Requirements";
            pondDetailUrl = "resource_xq_info.html?id=";
            type = "xq";
        } else if(id == "zj"){
            pondUrl = "/index/lib/experts";
            pondDetailUrl = "resource_zj_info.html?id=";
            type = "zj";
        } else if(id == "qy"){
            pondUrl = "/index/lib/Enterprises";
            pondDetailUrl = "resource_qy_info.html?id=";
            type = "qy";
        } else if(id == "cg"){
            pondUrl = "/index/lib/Harvests";
            pondDetailUrl = "resource_cg_info.html?id=";
            type = "cg";
        } else if(id == "ys"){
            pondUrl = "/index/lib/Colleges";
            pondDetailUrl = "resource_ys_info.html?id=";
            type = "ys";
        } else if(id == "fw"){
            pondUrl = "/index/lib/Services";
            pondDetailUrl = "resource_fw_info.html?id=";
            type = "fw";
        }
        searchPond();
    }

    /**
     * 查询文章内容
     * @param text
     */
    function searchArticle(paramData){
        if (!paramData) {
            paramData = {};
        }
        paramData.keyStr = searchText;
        paramData.loadAll = false;
        paramData.needPaging = true;
        paramData.currPage = paramData.currentPage ? paramData.currentPage : initCurrPage;
        paramData.pageSize = paramData.pageSize ? paramData.pageSize : initPageSize;
        let params = {
            url: "/index/articles",
            data: paramData,
            sCallback: function (data) {
                getArticleSuccessCallback(data)
            },
            eCallback: function (res) {
                getArticleSuccessCallback({rows:[]})
            }
        }
        requset(params);
    }
    function getArticleSuccessCallback(data){
        if(!data){
            data = {total:0,rows:[]};
        }
        var total = data.total;
        var pageSize = data.pageSize;
        let paginationParams = {total: total, pageSize: pageSize};
        if (!articlePaginationFlag) {
            articlePaginationFlag = true;
            paginationInit(paginationParams, changeArticlePagination);
        }
        $("#article_count").html(total);
        var codeHtml = "";
        $.each(data.rows, function (index, data) {
            var id = data.id;
            var title = data.title;
            var categoryCode = data.categoryCode;
            var description = data.description;
            codeHtml +="<div class='panel panel-default' style='margin:10px'>" +
                " <div class='panel-heading' style='background-color: white;'>" +
                "  <span style='text-decoration: underline;' >" +
                " <a href="+articleUrl+id+"&type="+categoryCode+">"+title+"</a> </span></div>" +
                "<div class='panel-body'>" + description+"</div></div>";
        })
        $("#search_content").html(codeHtml);
    }
    function changeArticlePagination(paginationParams){
        searchArticle(paginationParams)
    }

    /**
     * 查询活动的数量
     */
    function searchActivityCount(){
        var paramData = {};
        paramData.kw = searchText;
        paramData.loadAll = false;
        paramData.needPaging = true;
        paramData.currPage = initCurrPage;
        paramData.pageSize = initPageSize;
        let params = {
            url: "/index/exchanges",
            data: paramData,
            sCallback: function (data) {
                $("#hd_count").html(data.total);
            },
            eCallback: function (res) {
                $("#hd_count").html("0");
            }
        }
        requset(params);
    }

    /**
     * 查询活动内容
     * @param text
     */
    function searchActivity(paramData){
        if (!paramData) {
            paramData = {};
        }
        paramData.kw = searchText;
        paramData.loadAll = false;
        paramData.needPaging = true;
        paramData.currPage = paramData.currentPage ? paramData.currentPage : initCurrPage;
        paramData.pageSize = paramData.pageSize ? paramData.pageSize : initPageSize;
        let params = {
            url: "/index/exchanges",
            data: paramData,
            sCallback: function (data) {
                getActivitySuccessCallback(data)
            },
            eCallback: function (res) {
                getActivitySuccessCallback({rows:[]})
            }
        }
        requset(params);
    }
    function getActivitySuccessCallback(data){
        var total = data.total;
        var pageSize = data.pageSize;
        let paginationParams = {total: total, pageSize: pageSize};
        if (!activityPaginationFlag) {
            activityPaginationFlag = true;
            paginationInit(paginationParams, changeActivityPagination);
        }
        $("#hd_count").html(total);
        var codeHtml = "";
        $.each(data.rows, function (index, data) {
            var id = data.id;
            var title = data.title;
            var description = data.subject;
            codeHtml +="<div class='panel panel-default' style='margin:10px'>" +
                " <div class='panel-heading' style='background-color: white;'>" +
                "  <span style='text-decoration: underline;' >" +
                " <a href="+activityUrl+id+">"+title+"</a> </span></div>" +
                "<div class='panel-body'>" + description+"</div></div>";
        })
        $("#search_content").html(codeHtml);
    }
    function changeActivityPagination(paginationParams){
        searchActivity(paginationParams)
    }

    /**
     * 查询池子数量
     * @param type 池子类型
     * @param countId 池子数量ID
     */
    function searchPondCount(type,pondUrl,countId){
        var paramData = {};
        paramData.objName = searchText;
        paramData.loadAll = true;
        paramData.needPaging = true;
        paramData.currPage =initCurrPage;
        paramData.pageSize = initPageSize;
        let params = {
            url: pondUrl,
            data: paramData,
            sCallback: function (data) {
                $("#"+countId).html(data.total);
            },
            eCallback: function (res) {
            }
        }
        requset(params);
    }

    /**
     * 查询池子
     * @param text
     */
    function searchPond(paramData){
        if (!paramData) {
            paramData = {};
        }
        paramData.objName = searchText;
        paramData.loadAll = true;
        paramData.needPaging = true;
        paramData.currPage = paramData.currentPage ? paramData.currentPage : initCurrPage;
        paramData.pageSize = paramData.pageSize ? paramData.pageSize : initPageSize;
        let params = {
            url: pondUrl,
            data: paramData,
            sCallback: function (data) {
                getPondSuccessCallback(data)
            },
            eCallback: function (res) {
            }
        }
        requset(params);
    }
    function getPondSuccessCallback(data){
        var total = data.total;
        var pageSize = data.pageSize;
        let paginationParams = {total: total, pageSize: pageSize};
        if (!pondPaginationFlag) {
            pondPaginationFlag = true;
            paginationInit(paginationParams, changePondPagination);
        }
        if(type == "zj"){
            console.log("zj"+total);
            $("#zj_count").html(total);
        } else if(type == "xq"){
            console.log("xq"+total);
            $("#xq_count").html(total);
        } else if(type == "qy"){
            console.log("qy"+total);
            $("#qy_count").html(total);
        } else if(type == "cg"){
            console.log("cg"+total);
            $("#cg_count").html(total);
        } else if(type == "ys"){
            console.log("ys"+total);
            $("#ys_count").html(total);
        } else if(type == "fw"){
            console.log("fw"+total);
            $("#fw_count").html(total);
        }
        var codeHtml = "";
        $.each(data.rows, function (index, data) {
            var id = data.id;
            var title = data.name;
            var description = data.subject;
            var reqName = data.reqName;
            if (!reqName) {
                reqName = data.resName;
            }
            var domain = data.domain;
            var contentText = "";
            if(type == "zj"){
                var number = data.number;
                title = number + title;
                var school = data.school?data.school:"";
                var researchArea = data.researchArea?data.researchArea:"";
                var successRecord = data.successRecord?data.successRecord:"";
                var projectDesc = data.projectDesc?data.projectDesc:"";
                contentText = school+"<br/>"+researchArea+successRecord+projectDesc;
            } else if(type == "xq"){
                var number = data.number;
                title = number + title;
                var unitName = data.unitName?data.unitName:"";
                var country = data.country?data.country:"";
                var reqDesc = data.reqDesc?data.reqDesc:"";
                var reqNew = data.reqNew?data.reqNew:"";
                var reqOther = data.reqOther?data.reqOther:"";
                contentText = unitName+"<br/>"+reqDesc+reqNew+reqOther;
            } else if(type == "qy"){
                var number = data.number;
                title = number + title;
                var address = data.address?data.address:"";
                var product = data.product?data.product:"";
                var sharedResources = data.sharedResources?data.sharedResources:"";
                var jmrhTech = data.jmrhTech?data.jmrhTech:"";
                var jmrhSuggest = data.jmrhSuggest?data.jmrhSuggest:"";
                contentText = address+"<br/>"+product+sharedResources+jmrhTech+jmrhSuggest;
            } else if(type == "cg"){
                var number = data.number;
                title = number + title;
                var unitName = data.unitName?data.unitName:"";
                var resultsDes = data.resultsDes?data.resultsDes:"";
                var newIdea = data.newIdea?data.newIdea:"";
                var useArea = data.useArea?data.useArea:"";
                contentText = unitName+"<br/>"+resultsDes+newIdea+useArea;
            } else if(type == "ys"){
                var number = data.number;
                title = number + title;
                var address = data.address?data.address:"";
                var introduction = data.introduction?data.introduction:"";
                var majorPlatform = data.newIdea?data.majorPlatform:"";
                contentText = address+"<br/>"+introduction+majorPlatform;
            } else if(type == "fw"){
                var number = data.number;
                title = number + title;
                var address = data.address?data.address:"";
                var servicesProvided = data.servicesProvided?data.servicesProvided:"";
                contentText = address+"<br/>"+servicesProvided;
            }
            if (contentText != null && contentText != undefined && contentText.length > 53) {
                contentText = contentText.substring(0,53);
                contentText = contentText+ "......";
            }
            codeHtml +="<div class='panel panel-default' style='margin:10px'>" +
                " <div class='panel-heading' style='background-color: white;'>" +
                "  <span style='text-decoration: underline;' >" +
                " <a class='allbond' href='javascript:void(0)' info="+pondDetailUrl+id+">"+title+"</a> </span></div>" +
                "<div class='panel-body'>" + contentText +"</div></div>";

        })
        $("#search_content").html(codeHtml);
        // 点击池子
        $(".allbond").on("click",function(){
            // 没有登陆提示需要登陆、
            var isLogin = checkLogin();
            if (isLogin){
                var info =$(this).attr("info");
                var url = encodeURI(info);
                window.location.href = url;
            }else{
                console.log('需要登陆')
                $("#showModal").modal('show');
            }
        })
    }
    function changePondPagination(paginationParams){
        searchPond(paginationParams)
    }


    // 隐藏modal
    $("#confirmLogin").on("click", function () {
        $("#showModal").modal('hide');
    })

});


